--博文才智
local bowencaizhi = fk.CreateSkill{
    name =  "mcdh_bowencaizhi",
    frequency = Skill.Limited,
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

Fk:addPoxiMethod{
    name = "mcdh_bowencaizhi_getcard",
    card_filter = function(to_select, selected, data)
        if #selected == 0 then
            return true
        elseif #selected == 1 then
            return table.contains(data[1][2],to_select) ~= table.contains(data[1][2],selected[1])
        end
        return false
    end,
    feasible = function(selected)
        return #selected == 2
    end,
    prompt = function ()
        return Fk:translate("#bowencaizhi_getcard")
    end
}

bowencaizhi:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        if player:hasSkill(self) and player:usedSkillTimes(self.name,Player.HistoryGame) == 0  and not player:isKongcheng() then
            local targets = {}
            for _, move in ipairs(data) do
                if move.to ~= nil and move.toArea == Card.PlayerHand then
                    if #move.moveInfo > 1 and  move.to ~= player and move.to.phase ~= Player.Draw  then
                        table.insertIfNeed(targets,move.to)
                    end
                end
            end
            if #targets >= 1 then
                self.cost_data = targets
                return true
            end
        end
    end,
    on_trigger = function(self, event, target, player, data)
        local room = player.room
        local targets = self.cost_data
        room:sortByAction(targets)
        for _, p in ipairs(targets) do
            if player.dead or not player:hasSkill(self) and player:isKongcheng() and player:usedSkillTimes(self.name,Player.HistoryGame) > 0 then break end
            if not p.dead and not p:isKongcheng() then
                self:doCost(event, p, player, nil)
            end
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        return room:askToSkillInvoke(player, {skill_name=self.name,prompt="#bowencaizhi:"..target.id})
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        U.viewCards(target, player:getCardIds(Player.Hand), self.name, "#bowencaizhi_view:"..player.id)
        local playerhand = lib.SortCardbySuit(player:getCardIds(Player.Hand))
        local targethand = lib.SortCardbySuit(target:getCardIds(Player.Hand))
        local carddata = {}
        table.insert(carddata,{player.general,playerhand})
        table.insert(carddata,{target.general,targethand})
        local card =room:askToPoxi(player, {poxi_type="mcdh_bowencaizhi_getcard",data=carddata,cancelable=false})
        if table.contains(playerhand,card[1]) then
            playerhand = table.filter(playerhand,function(id) return Fk:getCardById(card[1]).suit == Fk:getCardById(id).suit  end)
            targethand = table.filter(targethand,function(id) return Fk:getCardById(card[2]).suit == Fk:getCardById(id).suit end)
        else
            playerhand = table.filter(playerhand,function(id) return Fk:getCardById(card[2]).suit == Fk:getCardById(id).suit  end)
            targethand = table.filter(targethand,function(id) return Fk:getCardById(card[1]).suit == Fk:getCardById(id).suit end)
        end

        U.swapCards(room, player, player, target, playerhand, targethand, self.name, Card.PlayerHand)
    end
})

Fk:loadTranslationTable {
    ["mcdh_bowencaizhi"] = "博文才智",
    [":mcdh_bowencaizhi"] = "限定技,当其他角色于其摸牌阶段外一次性获得至少两张牌后,你可以与其互相观看对方的手牌,然后用你手牌中一种花色的所有牌与其手牌中一种花色的所有牌交换。",
    ["#bowencaizhi_getcard"] = "博文才智：选择要要交换花色的牌",
    ["#bowencaizhi_view"] = "博文才智:观看 %src 的手牌",
    ["#bowencaizhi"] = "博文才智:对 %src 发动",
}

return bowencaizhi